/**
 */
package com.dingpeikeji.quickdeer.core.modules.office.web;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.office.entity.SysOffice;
import com.dingpeikeji.quickdeer.core.modules.office.service.SysOfficeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TOfficeCredit;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TOfficeCreditService;
import com.google.common.collect.Lists;

/**授信额度Controller
 */
@Controller
@RequestMapping(value = "${adminPath}/office/credit")
public class SysOfficeCreditController extends BaseController {

	@Autowired
	private SysOfficeService sysOfficeService;
	@Autowired
	private TOfficeCreditService tOfficeCreditService;
	
	@ModelAttribute
	public SysOffice get(@RequestParam(required=false) String id) {
		SysOffice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysOfficeService.get(id);
		}
		if (entity == null){
			entity = new SysOffice();
		}
		return entity;
	}
	
	@RequiresPermissions("rentproduct:tOfficeCredit:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysOffice sysOffice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysOffice> page = sysOfficeService.findPage(new Page<SysOffice>(request, response), sysOffice); 
		TOfficeCredit tOfficeCredit = new TOfficeCredit();
		Calendar calendar = Calendar.getInstance();
		tOfficeCredit.setCredityears(calendar.get(Calendar.YEAR));
		// 设置当前年条件内的总使用额度
		List<TOfficeCredit> officeYearCreditList = tOfficeCreditService.findTotalCreditList(tOfficeCredit);
		// 设置当前年月条件内的总使用额度
		tOfficeCredit.setCreditmonths(calendar.get(Calendar.MONTH)+1);
		List<TOfficeCredit> officeMonthCreditList = tOfficeCreditService.findTotalCreditList(tOfficeCredit);
		for(SysOffice office:page.getList()){
			if(officeYearCreditList!=null && !officeYearCreditList.isEmpty()) {
				for(TOfficeCredit credit:officeYearCreditList){
					if(office.getId().equals(credit.getOffice().getId())){
						office.setCreditInYear(credit.getTotalcredit());
						continue;
					}
				}
			}
			if(officeMonthCreditList!=null && !officeMonthCreditList.isEmpty()) {
				for(TOfficeCredit credit:officeMonthCreditList){
					if(office.getId().equals(credit.getOffice().getId())){
						office.setCreditInMonth(credit.getTotalcredit());
						continue;
					}
				}
			}
		}
		model.addAttribute("page", page);
		model.addAttribute("office", sysOffice);
		return "modules/rentproduct/tOfficeCreditList";
	}

	@RequiresPermissions("rentproduct:tOfficeCredit:view")
	@RequestMapping(value = "form")
	public String form(SysOffice sysOffice, Model model) {
		if(!StringUtils.isEmpty(sysOffice.getId())){
			TOfficeCredit tOfficeCredit = new TOfficeCredit();
			Calendar calendar = Calendar.getInstance();
			tOfficeCredit.setId(sysOffice.getId());
			tOfficeCredit.setCredityears(calendar.get(Calendar.YEAR));
			// 设置当前年条件内的总使用额度
			List<TOfficeCredit> officeYearCreditList = tOfficeCreditService.findTotalCreditList(tOfficeCredit);
			// 设置当前年月条件内的总使用额度
			tOfficeCredit.setCreditmonths(calendar.get(Calendar.MONTH)+1);
			List<TOfficeCredit> officeMonthCreditList = tOfficeCreditService.findTotalCreditList(tOfficeCredit);
			if(officeYearCreditList!=null && !officeYearCreditList.isEmpty()) {
				for(TOfficeCredit credit:officeYearCreditList){
					if(sysOffice.getId().equals(credit.getOffice().getId())){
						sysOffice.setCreditInYear(credit.getTotalcredit());
						continue;
					}
				}
			}
			if(officeMonthCreditList!=null && !officeMonthCreditList.isEmpty()) {
				for(TOfficeCredit credit:officeMonthCreditList){
					if(sysOffice.getId().equals(credit.getOffice().getId())){
						sysOffice.setCreditInMonth(credit.getTotalcredit());
						continue;
					}
				}
			}
		}
		model.addAttribute("office", sysOffice);
		return "modules/rentproduct/tOfficeCreditForm";
	}

	@RequiresPermissions("rentproduct:tOfficeCredit:edit")
	@RequestMapping(value = "save")
	public String save(SysOffice sysOffice, Model model, RedirectAttributes redirectAttributes) {
		List<String> messages = Lists.newArrayList();
		if(sysOffice.getMonthcredit() == null){
			messages.add("月授权额度不能为空");
		}
		if(sysOffice.getTotalcredit() == null) {
			messages.add("总授权额度不能为空");
		}
		if(sysOffice.getMonthcredit()>sysOffice.getTotalcredit()){
			messages.add("总授权额度不能小于月授权额度");
		}
		if(!messages.isEmpty()){
			addMessage(model, messages.toArray(new String[]{}));
			return form(sysOffice, model);
		}
		sysOfficeService.save(sysOffice);
		addMessage(redirectAttributes, "保存授信额度成功");
		return "redirect:"+Global.getAdminPath()+"/office/credit/?repage";
	}

}
